using AntDesign;
using AntDesign.Select.Internal;
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Forms;
using SE.PO.CInterface.CuCoding.Signals;
using SE.PO.CInterface.DBProtocols.Devices;
using SE.PO.CInterface.Web.Models;
using SE.PO.CInterface.Web.Pages.DB.Components;
using SE.PO.CInterface.Web.Pages.Form;

namespace SE.PO.CInterface.Web.Pages.DB.Device
{
    public partial class SyncDeviceFrom
    {
        
        private SelectEquips _selectEquips = null!;

        private List<SelectMode> _siteType = null!;

        private List<SelectMode> _nodeFeatures = null!;

        [Parameter] public EventCallback OnSuccess { get; set; }

        [Parameter] public EventCallback OnFailure { get; set; }

        [Parameter] public EventCallback OnCancel { get; set; }

        public Form<CreateUpdateDeviceDto> Form;

        public CreateUpdateDeviceDto Model = new();

        private readonly FormItemLayout _formItemLayout = new FormItemLayout
        {
            LabelCol = new ColLayoutParam
            {
                Xs = new EmbeddedProperty {Span = 24},
                Sm = new EmbeddedProperty {Span = 7},
            },

            WrapperCol = new ColLayoutParam
            {
                Xs = new EmbeddedProperty {Span = 24},
                Sm = new EmbeddedProperty {Span = 12},
                Md = new EmbeddedProperty {Span = 10},
            }
        };

        private readonly FormItemLayout _submitFormLayout = new FormItemLayout
        {
            WrapperCol = new ColLayoutParam
            {
                Xs = new EmbeddedProperty {Span = 24, Offset = 0},
                Sm = new EmbeddedProperty {Span = 10, Offset = 7},
            }
        };

        protected override async Task OnInitializedAsync()
        {
          
        }
        
        private async Task OnFinish()
        {
            try
            {
                var equips = _selectEquips.SelectedEquips;
                var devices = from e in equips
                    select new CreateUpdateDeviceDto()
                    {
                        ScId = Model.ScId,
                        DeviceType = Model.DeviceType,
                        Order = e.Order,
                        SiteId = Model.SiteId,
                        RoomId = Model.RoomId,
                        SuId = Model.SuId,
                        IODevice = e.IODevice,
                        Equipment = e.Name,
                        DeviceDesc = $"{e.Cluster}|{e.Name}|{e.IODevice}".PadRight(60, ' ').Substring(0, 60),
                    };
                var ok = await Client.SyncDevicess(devices);
                if (Form.Validate() && ok)
                {
                    await OnSuccess.InvokeAsync();
                }
                else
                {
                    await OnFailure.InvokeAsync();
                }
            }
            catch (System.Exception ex)
            {
                await Notice.Error(new()
                {
                    Duration = 100,
                    Message = "异常",
                    Description = ex.Message
                });
            }
        }

        private async Task OnFinishFailed(EditContext editContext)
        {
            await OnCancel.InvokeAsync();
        }

        private async Task GetDeviceSourceByTypeAsync(string s)
        {
            await _selectEquips.GetSourceAsync(deviceType:s);
        }
    }
}